<template>
  <section>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters" ref="formRef">
        <el-form-item label="部门名称">
          <el-input v-model="filters.keyword" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" >
          <el-select v-model="filters.parentId" placeholder="请选择上级部门">
            <el-option v-for="item in optionDeptData" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button type="danger" @click="resetForm('formRef')">重置</el-button>
          <el-button type="primary" @click="addData">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="tableData"
              highlight-current-row
              @selection-change="selsChange"
              border
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号">
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="190">
      </el-table-column>
      <el-table-column prop="managerName" label="部门经理" width="190">
      </el-table-column>
      <el-table-column prop="parentName" label="上级部门" width="190">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="150">
        <!-- 如果需要对表中某个字段进行显示内容的切换，则可以使用<template>语法进行转换 -->
        <!-- 分析：如果是1正常；如果是0：禁用-->
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
          <el-tag size="mini" v-if="scope.row.state == 1" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--弹框界面-->
    <el-dialog :title="titleValue" :visible.sync="dialogTableVisible">
      <el-form :model="addForm" label-width="100px" :rules="formRules" ref="addForm">
<!--        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="addForm.sex">
            <el-radio class="radio" :label="1">男</el-radio>
            <el-radio class="radio" :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker type="date" placeholder="选择日期" v-model="addForm.birth"></el-date-picker>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="addForm.addr"></el-input>
        </el-form-item>-->

        <el-form-item label="部门编号" prop="sn">
          <el-input type="text"  v-model="addForm.sn"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input type="text" v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="部门状态" prop="state">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="1">正常</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="部门经理" prop="managerName">
          <el-select v-model="addForm.managerId"  value-key="id" placeholder="请选择">
            <el-option
                v-for="emp in optionEmplData"
                :label="emp.username"
                :value="emp.id">
              <span style="float: left">{{ emp.username }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="上级部门" prop="parentName">
          <el-select v-model="addForm.parentId"  value-key="id" placeholder="请选择">
            <el-option
                v-for="dept in optionDeptData"
                :label="dept.name"
                :value="dept.id">
              <span style="float: left">{{ dept.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dialogTableVisible = false">取消</el-button>
        <el-button type="primary" @click.native="onSubmit">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--
        @size-change：选择了每页条数 ，触发handleSizeChange方法
        @current-change：选择了第几页 ，触发handleCurrentChange方法
        :@current-change：把当前页绑定到  current 变量
        :page-size="size" ：每页显示的条数
        :layout  ：分页条布局
        :total="tableTotal" ：总条数
     -->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="filters.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="filters.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
  import {batchRemoveUser} from "../../api/api";

  export default {
    name: 'Department',
    data() {
      return {
        titleValue: '',
        filters:{
          keyword: '',
          current: 1, //默认第几页
          size: 10, //默认每页显示条数
        },
        tableData: [],
        optionDeptData: [], //下拉部门选项集合
        optionEmplData: [], // 下拉员工集合
        total: 0,//列表总数量
        dialogTableVisible: false, //表单弹框是否展示
        sels: [],//列表选中列数据
        formRules: { //表单校验规则
          name: [
            { required: true, message: '请输入部门名称', trigger: 'blur' }
          ]
        },
        addForm:{
        },//表单对象
      }
    },
    //定义所有的方法
    methods: {
      //点击复选框时触发
      selsChange(sels) {
        this.sels = sels;
      },
      //重置表单
      resetForm() {
        this.filters = {};
      },
      //点击编辑按钮触发
      handleEdit(index, row){
        this.titleValue = '编辑部门';
        //显示弹框
        this.dialogTableVisible = true;
        this.addForm = Object.assign({}, row);//属性拷贝
      },
      //提交表单
      onSubmit(){
        if(this.addForm.id){
          //id有值，做修改
          //发请求
          this.$http.post("/dept/update", this.addForm).then(res=>{
            //隐藏弹框
            this.dialogTableVisible = false;
            //刷新列表
            this.getTableData();
            //提示信息
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }else{
          //id没有值，做新增操作
          //发请求
          this.$http.post("/dept/add", this.addForm).then(res=>{
            //隐藏弹框
            this.dialogTableVisible = false;
            //刷新列表
            this.getTableData();
            //提示信息
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }
      },
      //点击新增
      addData(){
        //显示弹框
        this.dialogTableVisible = true;
        this.titleValue = '新增部门';
        //清空表单数据
        this.addForm = {};
      },
      //删除
      handleDelete(index, row){
        this.$confirm('确定删除此记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //点击确定执行下面代码
          //发请求
          this.$http.delete("/dept/delete/" + row.id, null).then(res=>{
            this.getTableData();//刷新列表数据
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }).catch(error => {
          console.log(error);
        });
      },
      //批量删除
      batchRemove() {
        //从sels中获取id值形成集合，传给后台做批量删除
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          let param = { ids: ids };
          //发请求
          this.$http.post("/dept/batchDelete", param).then(res=>{
            this.getTableData();//刷新列表数据
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }).catch(error => {
          console.log(error);
        });
      },
      //获取列表数据
      getTableData(){
        //发请求
        this.$http.post("/dept/query", this.filters).then(res=>{
          if(res.data.success == true){
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
          }
        }).catch(error => {
          console.log(error);
        });
      },
      //选择每页条数时触发
      handleSizeChange(val){
        console.log('选择了每页条数=' + val);
        this.filters.size = val;
        this.getTableData();
      },
      //翻页触发该函数
      handleCurrentChange(val){
        console.log('翻页了=' + val);
        this.filters.current = val;
        this.getTableData();
      },
      //提供一个查询方法：查询部门表中所有的部门列表，然后给前端进行【上级部门的查询显示】
      findAllDept(){
        this.$http.post('/dept/all').then(res=>{
          this.optionDeptData = res.data.data;
        }).catch(error => {
          console.log(error);
        });
      },
      // 查询所有员工的对象数据
      findAllEmpl(){
        this.$http.post('/empl/all').then(res=>{
          this.optionEmplData = res.data.data;
        }).catch(error => {
          console.log(error);
        });
      }
    },

    //钩子函数：数据初始化
    mounted() {
      this.getTableData();
      //查询所有部门信息
      this.findAllDept();
      // 查询所有员工对象
      this.findAllEmpl();
    }
  }
</script>

<style scoped>
</style>
